export async function typeAndEnter(id, infoId, interval) {
    console.log(interval, 'interval');
    console.log(infoId, 'infoId');


    const button = await window.getElementNodeValue('person.addFriend')
    console.log(button, 'button');
    const label = button.getAttribute('aria-label')
    console.log(label, 'label');
    const labelList = ['添加好友', 'Tambahkan teman']
    if (!button || !labelList.includes(label)) {
        console.log('未找到新聊天按钮');
        const baseUrl = 'https://mv.bckj.site/basic-api'
        const url = `${baseUrl}/sys/task/user/add/history/update/add/friend/status`;
        // status 1,已完成    2，未同意，   3，无法添加
        const params = {
            accountId: id,
            infoId: infoId,
            status: 3
        }
        const response = await fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(params)
        });

        if (!response.ok) {
            console.log("/sys/task/user/add/history/update/add/friend/status 接口请求失败:", response.status, response.statusText);
            return;
        }

        const data = await response.json();
        if (data.code !== 200) {
            console.log("/sys/task/user/add/history/update/add/friend/status 接口请求失败:", data);
            return;
        }

        console.log("/sys/task/user/add/history/update/add/friend/status 接口请求成功:", data);
        return;
    }


    button.click();
    setTimeout(async () => {
        const dialog = await window.getElementNodeValue('person.dialog')
        console.log(dialog, 'dialog');

        if (dialog) {
            // clearInterval(interval)
            console.log(dialog, 'dialog');
            // 出现那个提示框的时候不要加人了，否则会封号，出现弹框后逻辑
            const baseUrl = 'https://mv.bckj.site/basic-api'
            const url = `${baseUrl}/sys/user/facebook/update`;
            const params = {
                id: id,
                status: 0
            }
            const response = await fetch(url, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json"
                },
                body: JSON.stringify(params)
            });

            if (!response.ok) {
                console.log("/sys/user/facebook/update 接口请求失败:", response.status, response.statusText);
                return;
            }

            const data = await response.json();
            if (data.code !== 200) {
                console.log("/sys/user/facebook/update 接口请求失败:", data);
                return;
            }

            console.log("/sys/user/facebook/update 接口请求成功:", data);
        }
    }, 2000);

}

export async function submitSet(formData) {
    console.log(formData, 'formData');
    if (!formData.uname || !formData.facebookName || !formData.facebookPassword) {
        console.log('请填写必填字段');
        return
    }
    const baseUrl = 'https://mv.bckj.site/basic-api'
    const apiUrl = `${baseUrl}/sys/user/facebook/add`;




    console.log("请求API地址:", apiUrl);

    const response = await fetch(apiUrl, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(formData)
    });

    if (!response.ok) {
        console.log("/sys/user/facebook/add 接口请求失败:", response.status, response.statusText);
        return;
    }

    const data = await response.json();
    if (data.code !== 200) {
        console.log("/sys/user/facebook/add 接口请求失败:", data);
        return;
    }

    console.log("/sys/user/facebook/add 接口请求成功:", data);
    showMessage('提交成功', 'success');
}

window.typeAndEnter = typeAndEnter;
window.submitSet = submitSet;



console.log('popup.js 模块已加载到全局作用域');
console.log('typeAndEnter已设置为:', window.typeAndEnter);
